// ==========================================================================
// App.cs
// Silverlight Shapes Library
// ==========================================================================
// Copyright (c) Silverlight Shapes Development Group
// All rights reserved.
// ==========================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using System.Globalization;
using System.Linq;
using System.Windows;
using SD.Samples.Configuration;
using System.Diagnostics;
using SD.Utils.Windows;

namespace SD.Samples
{
    /// <summary>
    /// Defines the application class, the entry point for our sample browser.
    /// </summary>
    public partial class App : Application
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="App"/> class.
        /// </summary>
        public App()
        {
            Startup += Application_Startup;

            UnhandledException += Application_UnhandledException;

            InitializeCompositions();
            InitializeComponent();            
        }

        #endregion

        #region Methods

        /// <summary>
        /// Initializes the compositions using the managed extensibility framework..
        /// </summary>
        private static void InitializeCompositions()
        {
            List<ComposablePartCatalog> catalogs = new List<ComposablePartCatalog>();

            // Load all exports from the current deployment.
            DeploymentCatalog deploymentCatalog = new DeploymentCatalog();
            deploymentCatalog.DownloadAsync();

            catalogs.Add(deploymentCatalog);

            // Load the xap folders for all projects.
            foreach (Sample sample in ApplicationSamples.Default.SampleCatagories.SelectMany(x => x.Samples))
            {
                if (!string.IsNullOrEmpty(sample.ProjectFile))
                {
                    string xapPath = string.Format(CultureInfo.CurrentCulture, "{0}.xap", sample.ProjectFile);

                    deploymentCatalog = new DeploymentCatalog(new Uri(xapPath, UriKind.Relative));

                    deploymentCatalog.DownloadCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(deploymentCatalog_DownloadCompleted);
                    deploymentCatalog.DownloadAsync();

                    catalogs.Add(deploymentCatalog);
                }
            }

            AggregateCatalog aggregateCatalog = new AggregateCatalog(catalogs);

            CompositionHost.Initialize(new CompositionContainer(aggregateCatalog));
        }

        static void deploymentCatalog_DownloadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
        }


        /// <summary>
        /// Handles the Startup event of the Application object.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.StartupEventArgs"/> instance containing the event data.</param>
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            RootVisual = new MainPage();
        }

        /// <summary>
        /// Handles the UnhandledException event of the Application object.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.ApplicationUnhandledExceptionEventArgs"/> instance containing the event data.</param>
        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            // If the app is running outside of the debugger then report the exception using
            // a ChildWindow control.
            if (!Debugger.IsAttached)
            {
                // This will allow the application to continue running after an exception has been thrown
                // but not handled. 
                // For production applications this error handling should be replaced with something that will 
                // report the error to the website and stop the application.
                ErrorWindow.Show(e.ExceptionObject.Message);

                e.Handled = true;
            }
        }

        #endregion
    }
}